﻿package {
	import flash.display.*;
	import flash.events.TimerEvent;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.utils.Timer;
	import flash.filters.*;
	import flash.geom.Rectangle;

	class TheEnd extends Sprite {
		private var tree:Tree;
		private var treeTimer:Timer;
		private var mcTimer:Timer;
		private var mcArray:Array;
		private var filter1:GradientGlowFilter;
		private var glowSpeed:Number;
		private var glowflag:int;
		private var xmasmp3:XmasSound;
		private var mcRemoved:int;
		private var clickedmc:MovieClip;
		private var santa:Santa;
		private var santaskating:santaSkating;
		private var santaTimer:Timer;
		private var santaplaying:Boolean;
		private var room:TwoIcons;
		private var man:TwoIcons;
		private var endmp3:WeWishYouAMerryChristmas;

		public function TheEnd(endtree:Function):void {
			mcArray=new Array(18,3,8,14,1,20,19,9,7);
			mcRemoved=mcArray.length-1;
			var colors:Array=[0xFFFFFF,0xFFFFFF,0xFFFFFF];
			var alphas:Array=[0,.5,1];
			var ratios:Array=[0,128,225];
			filter1=new GradientGlowFilter(0,0,colors,alphas,ratios,0,0,1,1,"inner",false);
			glowflag=0;
			glowSpeed=1;
			santa=new Santa();
			santa.x=450;
			santa.y=250;
			santa.gotoAndStop(5);
			santaplaying=false;
			santa.buttonMode=true;
			santa.addEventListener(MouseEvent.MOUSE_DOWN,mousedown);
			santa.addEventListener(MouseEvent.MOUSE_UP,mouseup);
			santaskating=new santaSkating();
			santaskating.x=-49;
			santaskating.y=236;
			santaskating.buttonMode=true;
			santaskating.stop();
			santaskating.addEventListener(MouseEvent.MOUSE_OVER,mouseover);
			addChild(santaskating);
			room=new TwoIcons  ;
			room.x=36;
			room.y=328;
			room.width=42;
			room.height=42;
			room.gotoAndStop(15);
			man=new TwoIcons  ;
			man.x=453;
			man.y=353;
			man.width=24;
			man.height=24;
			man.gotoAndStop(17);

			endmp3=new WeWishYouAMerryChristmas();
			initTree();
			initTimer();
			this.addEventListener(Game.ENDTREE.type, endtree);
		}
		private function initTree():void {
			tree=new Tree();
			tree.x=275;
			tree.y=180;
			tree.alpha=0;
			var i:uint;
			for (i=0; i<mcArray.length; i++) {
				tree["mc"+mcArray[i]].gotoAndStop(mcArray[i]);
				if (i>0) {
					tree["mc"+mcArray[i]].buttonMode=true;
					tree["mc"+mcArray[i]].addEventListener(MouseEvent.CLICK, onclick);
				}
			}
			addChild(tree);
		}
		private function initTimer():void {
			treeTimer=new Timer(10);
			treeTimer.addEventListener(TimerEvent.TIMER, treeTiming);
			treeTimer.start();
			mcTimer=new Timer(20,20);
			mcTimer.addEventListener(TimerEvent.TIMER, mcdisappear);
			mcTimer.addEventListener(TimerEvent.TIMER_COMPLETE, resetmc);
			santaTimer=new Timer(15);
			santaTimer.addEventListener(TimerEvent.TIMER,santatimer);
			santaTimer.start();
		}
		private function onclick(e:MouseEvent):void {
			if (mcTimer.running) {
				return;
			}
			clickedmc=e.target;
			xmasmp3=new XmasSound();
			xmasmp3.MP3.play();
			mcTimer.reset();
			mcTimer.start();
		}
		private function mcdisappear(e:TimerEvent):void {
			clickedmc.alpha-=0.05;
		}
		private function resetmc(e:TimerEvent):void {
			mcRemoved--;
			var i:uint;
			clickedmc.visible=false;
			//trace("mcremoved",mcRemoved);
			if (mcRemoved<=0) {
				mcRemoved=mcArray.length-1;
				for (i=0; i<mcArray.length; i++) {
					tree["mc"+mcArray[i]].visible=true;
					tree["mc"+mcArray[i]].alpha=1;
				}
			}
		}
		private function mousedown(e:MouseEvent):void {
			santa.startDrag(false,new Rectangle(0,0,500,350));
		}
		private function mouseup(e:MouseEvent):void {
			santa.stopDrag();
			changesanta();
		}
		private function mouseover(e:MouseEvent):void {
			var mc:MovieClip=e.currentTarget;
			mc.gotoAndPlay(mc.currentFrame-1);
		}
		private function santatimer(e:TimerEvent):void {
			if (santaskating.currentFrame==santaskating.totalFrames) {
				santaskating.gotoAndStop(1);
			}
			if (santaskating.currentFrame==1&&Math.random()<.002) {
				santaskating.nextFrame();
			}
			if (santaskating.currentFrame>1) {
				santaskating.nextFrame();
			}
			if (santaskating.hitTestObject(santa)) {
				santaskating.gotoAndStop(santaskating.currentFrame-2);
			}
			if (santa.stage==null&&Math.random()<.005) {
				addChild(santa);
			}
			if (room.stage==null&&Math.random()<.005) {
				addChild(room);
			}
			if (man.stage==null&&Math.random()<.005) {
				addChild(man);
			}
			if (Math.random()<.002) {
				changesanta();
			}
		}
		private function changesanta():void{
			if (santaplaying) {
				santa.stop();
			} else {
				santa.play();
			}
			santaplaying=! santaplaying;
		}
		private function treeTiming(e:TimerEvent):void {
			tree.alpha+=.01;
			if (tree.alpha>=1) {
				treeTimer.removeEventListener(TimerEvent.TIMER, treeTiming);
				treeTimer.addEventListener(TimerEvent.TIMER, treeTiming2);
				endmp3.play();
				this.dispatchEvent(Game.ENDTREE);
			}
		}
		private function treeTiming2(e:TimerEvent):void {
			var i:int;
			if (glowflag==2||glowflag==-30) {
				glowSpeed=- glowSpeed;
			}
			glowflag+=glowSpeed;
			filter1.blurX+=glowSpeed;
			filter1.blurY+=glowSpeed;

			for (i=0; i<mcArray.length; i++) {
				tree["mc"+mcArray[i]].filters=null;
				if (Math.random()<.03) {
					tree["mc"+mcArray[i]].filters=[filter1];
				}
			}
		}
	}

}